﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataContext;
using WorkFlowBLL;

namespace QLCF.Forms.Reports
{
    public partial class frmBillReport : Form
    {
        public frmBillReport()
        {
            InitializeComponent();
            bindAdmin();
        }     

        private void btnSearch_Click(object sender, EventArgs e)
        {
            CtrProduct ctrp = new CtrProduct();
            grvBill.AutoGenerateColumns = false;
            long? sumMoneyAll = 0;
            long? priceServicesAll = 0;
            long? promotionMoneyAll = 0;
            long? totalMoneyAll = 0;
            grvBill.DataSource = ctrp.GetBillReport(dateTimePicker1.Value, dateTimePicker2.Value, 
                Convert.ToInt32(cbxListUser.SelectedValue),
                ref sumMoneyAll, ref priceServicesAll, ref promotionMoneyAll, ref totalMoneyAll);
            lblSumMoneyAll.Text = (sumMoneyAll ?? 0)==0?"0 VNĐ":(sumMoneyAll ?? 0).ToString("0,0") + " VNĐ";
            lblPriceServicesAll.Text = (priceServicesAll ?? 0)==0?"0 VNĐ":(priceServicesAll ?? 0).ToString("0,0") + " VNĐ";
            lblPromotionMoneyAll.Text = (promotionMoneyAll ?? 0)==0?"0 VNĐ":(promotionMoneyAll ?? 0).ToString("0,0") + " VNĐ";
            lblTotalMoneyAll.Text = (totalMoneyAll ?? 0)==0?"0 VNĐ":(totalMoneyAll ?? 0).ToString("0,0") + " VNĐ";

        }

        private void bindAdmin()
        {
            CtrAdmin ctra = new CtrAdmin();
            cbxListUser.ValueMember = "AdminID";
            cbxListUser.DisplayMember = "FullName";
            List<uspAdminGetListAllResult> items = ctra.GetListAdminAll();
            uspAdminGetListAllResult all = new uspAdminGetListAllResult();
            all.AdminID = -1;
            all.UserName = "Tất cả";
            all.FullName = "Tất cả";
            items.Insert(0, all);
            cbxListUser.DataSource = items;
        }

        private void grvBill_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                var row = e.RowIndex;
                int billID = Convert.ToInt32(grvBill.Rows[row].Cells["BillID"].Value);
                CtrProduct ctrp = new CtrProduct();
                grvBillDetails.AutoGenerateColumns = false;
                grvBillDetails.DataSource = ctrp.BillDetailsGetByBillID(billID);
                var info = ctrp.BillGetInfoCommon(billID);
                lblSumMoney.Text = info.SumMoney==0?"0 VNĐ":info.SumMoney.ToString("0,0") + " VNĐ";
                lblPriceServices.Text = info.PriceServices==0?"0 VNĐ":info.PriceServices.ToString("0,0") + " VNĐ";
                lblPromotionMoney.Text = info.PromotionMoney==0?"0 VNĐ":info.PromotionMoney.ToString("0,0") + " VNĐ";
                lblTotalMoney.Text = info.TotalMoney==0?"0 VNĐ":info.TotalMoney.ToString("0,0") + " VNĐ";
            }
            catch (Exception loi)
            {
                MessageBox.Show(loi.Message.ToString());
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //frmViewBillReport frm1 = new frmViewBillReport(dateTimePicker1.Value,dateTimePicker2.Value, Convert.ToInt32(cbxListUser.SelectedValue));
            //frm1.ShowDialog();

            frmViewReportBill frmv = new frmViewReportBill(dateTimePicker1.Value, dateTimePicker2.Value, Convert.ToInt32(cbxListUser.SelectedValue));
            frmv.ShowDialog();
        }
    }
}
